package com.jqb.jspcart.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jqb.jspcart.bean.Users;
import com.jqb.jspcart.service.impl.CartServiceImpl;
import com.jqb.jspcart.service.impl.UserServiceImpl;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String opt = request.getParameter("opt");
		UserServiceImpl userServiceImpl = new UserServiceImpl();
		String realcode = (String) request.getSession().getAttribute("checkcode");
		Users user = null;
		if (opt.equals("login")) {
			String name = request.getParameter("name");
			String password = request.getParameter("password");
			String checkcode = request.getParameter("checkcode");
			
			boolean flag = false;
			
			if(name!=null&&password!=null&&checkcode.equals(realcode)){
				user = new Users();
				user.setName(name);
				user.setPassword(password);
				try {
					flag = userServiceImpl.isLogin(user);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(flag){
				request.getSession().setAttribute("user", name);
				request.getSession().setAttribute("cart", CartServiceImpl.newInstance());
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}else if(!checkcode.equals(realcode)){
				request.setAttribute("msg", "验证码错误...");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}else{
				request.setAttribute("msg", "用户名或密码错误...");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		}
		
		if (opt.equals("regist")) {
			String name = request.getParameter("name");
			String password = request.getParameter("password");
//			String repassword = request.getParameter("repassword");
//			String telephone = request.getParameter("telephone");
//			String email = request.getParameter("email");
//			String address = request.getParameter("address");
			String checkcode = request.getParameter("checkcode");
			
			boolean flag = false;
			
			if(null!=name&&null!=password&&realcode.equals(checkcode)){
				user = new Users();
				user.setName(name);
				user.setPassword(password);
				try {
					flag = userServiceImpl.addUser(user);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(flag){
				request.getSession().setAttribute("user", name);
				request.getSession().setAttribute("cart", CartServiceImpl.newInstance());
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}else if(!realcode.equals(checkcode)){
				request.setAttribute("msg", "验证码错误...");
				request.getRequestDispatcher("regist.jsp").forward(request, response);
			}else {
				request.setAttribute("msg", "用户名已被注册...");
				request.getRequestDispatcher("regist.jsp").forward(request, response);
			}
		}
		
		if (opt.equals("exit")) {
			request.getSession().removeAttribute("user");
			request.getSession().removeAttribute("cart");
			request.getSession().removeAttribute("hardware");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		
	}
}
